/**
 * KopsoxApplicationServices
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.services.security;

import java.util.HashSet;
import java.util.Set;


/**
 * KopsoxPrincipal is a User in an application. A user can have roles and users can be in groups
 * 
 * @author Konrad Renner
 *
 */
public final class KopsoxPrincipalImpl implements KopsoxPrincipal {
	
	/**
	 * serialVersionUID = 2131901704091202300L
	 */
	private static final long serialVersionUID = 2131901704091202300L;
	
	private final String userId;
	private final String name;
	private final Set<Role> roles;
	private final int hashCode;
	
	public KopsoxPrincipalImpl(String userId, String name) {
		this.name = name;
		this.userId = userId;
		this.roles = new HashSet<Role>();
		this.hashCode = 31 * 17 + this.userId.hashCode();
	}
	
	@Override
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		sb.append("KopsoxPrincipalImpl [userId=");
		sb.append(userId);
		sb.append(", physicalName=");
		sb.append(name);
		sb.append(", roles=");
		sb.append(roles);
		sb.append(']');
		
		return sb.toString();
	}
	

	@Override
	public int hashCode() {
		return hashCode;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		KopsoxPrincipalImpl other = (KopsoxPrincipalImpl) obj;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		return true;
	}



	/* (non-Javadoc)
	 * @see java.security.Principal#getName()
	 */
	@Override
	public String getName() {
		return this.userId;
	}

	@Override
	public boolean hasRole(Role role) {
		return roles.contains(role);
	}

	@Override
	public String getPhysicalName() {
		return this.name;
	}

	@Override
	public boolean addRole(Role role) {
		return this.roles.add(role);
	}

	@Override
	public boolean addRoles(Set<Role> role) {
		return this.roles.addAll(role);
	}

	@Override
	public boolean removeRole(Role role) {
		return this.roles.remove(role);
	}

	@Override
	public boolean removeRoles(Set<Role> role) {
		return this.roles.removeAll(role);
	}
}
